package com.kgcx.organization.service;

import com.github.pagehelper.PageHelper;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.constant.Cons;
import com.kgcx.dbutils.domain.BaseDomain;
import com.kgcx.dbutils.utils.PageUtils;
import com.kgcx.organization.domain.Company;
import com.kgcx.organization.mapper.CompanyMapper;
import com.kgcx.organization.rabbitmq.producer.CompanyProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CompanyService {
    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private CompanyProducer companyProducer;

    /**
     * 根据ID查询公司服务机构放到MQ里面给监管平台
     *
     * @param kid
     * @return
     */
    public Company getCompany(Long kid) {
        Company company = companyMapper.selectByPrimaryKey(kid);
        companyProducer.addCompany(company);
        return company;
    }

    public List<Company> getCompanyList(RequestData rd) {
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());

        return companyMapper.selectActive(BaseDomain.buildSelectDomain(rd, new Company()));
    }

    @Transactional(rollbackFor = Exception.class)
    public void addCompany(Company company) {
        companyMapper.insertSelective(company);

        companyProducer.addCompany(company);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateCompany(Company company) {
        companyMapper.updateByPrimaryKeySelective(company);

        companyProducer.updateCompany(company);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeCompany(Long kid) {
        Company company = new Company();
        company.setKid(kid);
        company.setStatus(Cons.COMMON_STATUS_DELETE);
        companyMapper.updateByPrimaryKeySelective(company);

        companyProducer.deleteCompany(company);
    }
}
